//
//  JSTLoadFailView.m
//  KYKJPackDemo
//
//  Created by 01 on 16/11/6.
//  Copyright © 2016年 蔡正军. All rights reserved.
//

#import "JSTLoadFailView.h"

typedef void(^refreshBlock)();

@interface JSTLoadFailView ()

@property (nonatomic, strong) refreshBlock block;

@end

@implementation JSTLoadFailView
- (void)dealloc
{
    self.block = nil;
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake((kDeviceWidth - kCurrentScreenWidth(120))/2., kDeviceHeight / 2. + kCurrentScreenWidth(25), kCurrentScreenWidth(120), kCurrentScreenWidth(50));
        [btn setTitle:@"重新加载" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(refreshWebView) forControlEvents:UIControlEventTouchUpInside];
        [btn.layer setMasksToBounds:YES];
        [btn.layer setCornerRadius:10.];
        [btn setBackgroundColor:RGBCOLOR(79, 165, 229)];
        [self addSubview:btn];
    }
    return self;
}

- (void) createContent:(void(^)())refreshBlock
{
    self.block = ^(){
        refreshBlock();
        return;
    };
}

- (void) refreshWebView
{
    if (self.block) {
        self.block();
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
typedef void(^agreeBlock)();
@interface CustomAlertView ()

@property (nonatomic, strong) NSString *disclaimerStr;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UILabel *contentLabel;

@property (nonatomic, strong) agreeBlock agBlock;

@end

@implementation CustomAlertView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self disclaimerStr];
    }
    return self;
}

- (NSString *) disclaimerStr
{
    NSString *nameStr = [JSTSystemInfo applicationName];
    NSString *mailboxStr = [JSTSystemInfo userEmail];
    NSString *qqStr = [JSTSystemInfo userQQ];
    _disclaimerStr = [NSString stringWithFormat:@"    %@提醒您:"
                               "在使用%@(本软件)功能之前，请您务必仔细阅读以下内容，若您点击“接受”，表示您对申明内容的知悉或认可；若您点击“拒绝”，系统将自动退出本软件。\n\n"
                               "一、关于本软件\n"
                               "    %@是一款手机播放视频的软件，可播放指定网络地址的视频，受中华人民共和国相关法律保护。\n\n"
                               "二、关于内容\n"
                               "    %@所集合内容均来自互联网，内容版权均归各电视台及版权方所有。%@不存储，不修改节目内容。视频数据流也不经由本公司服务器中转或存储。%@的目的是帮助全球华人收看到中国内地官方电视台节目。如果您喜欢节目内容，建议您下载相应的app观看正版授权内容。\n\n"
                               "三、关于分类\n"
                               "    %@对网络视频的聚合及分类，是根据用户的收看习惯做的浏览引导。这样可以方便用户更快捷的找到相应的视频。本软件并没有对电视频道做编辑及整理。\n\n"
                               "四、法律责任\n"
                               "    因本软件设计相关数据均来源或指向不受本公司控制的第三方网站，顾本公司对播放内容的安全性，合法性不做任何保证，也不承担任何责任。用户应对使用本软件所获得或发出的数据，以及使用软件所发布的言论与信息，自行承担法律责任。\n\n"
                               "五、权利及通知\n"
                               "    任何单位或个人认为本软件聚合到的第三方网站所呈现的内容可能涉嫌侵犯其信息网络传播权或其他权利的，应及时以书面方式通知本公司，并提供身份证明。本公司将在收到前述文件并经审查后，已发尽快断开相关链接内容。\n\n"
                               "本公司通讯方式如下：\n"
                               "联系邮箱：%@\n"
                               "联系QQ：%@\n",nameStr ,nameStr, nameStr, nameStr, nameStr ,nameStr,nameStr, mailboxStr, qqStr];
    
    return _disclaimerStr;
}

- (void) createContent:(void (^)())agreeBlock
{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kCurrentScreenWidth(10), 0, self.width, kCurrentScreenWidth(50))];
    titleLabel.text = @"免责申明";
    titleLabel.font = kSystemFont(15);
    [self addSubview:titleLabel];
    
    DLog(@"-----alertView.width = %f", self.width);
    
    CGSize size = [self sizeWithString:[self disclaimerStr] font:kSystemFont(12) CGsize:CGSizeMake(self.width - (2*kCurrentScreenWidth(5)), MAXFLOAT)];
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kCurrentScreenWidth(50), self.width, self.height - kCurrentScreenWidth(50)*2)];
    _scrollView.contentSize = CGSizeMake(self.width, size.height);
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.frame = CGRectMake(kCurrentScreenWidth(5), 0, self.width - 2*kCurrentScreenWidth(5), size.height);
    _contentLabel.text = _disclaimerStr;
    _contentLabel.numberOfLines = 0;
    _contentLabel.font = kSystemFont(12);
    _contentLabel.textColor = [UIColor blackColor];
    
    [_scrollView addSubview:_contentLabel];
    [self addSubview:_scrollView];
    
    NSArray *arr = @[@"拒绝", @"同意"];
    for (int i = 0; i < 2; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 100+i;
        btn.frame = CGRectMake(kCurrentScreenWidth(10) + i*((self.width-kCurrentScreenWidth(10))/2), CGRectGetMaxY(_scrollView.frame)+kCurrentScreenWidth(2.5), (self.width - kCurrentScreenWidth(10)*3)/2, kCurrentScreenWidth(45));
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.layer.cornerRadius = kCurrentScreenWidth(5);
        btn.layer.borderWidth = .5;
        btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self addSubview:btn];
    }
    _agBlock = ^(){
        agreeBlock();
    };
}

- (void) onClick:(UIButton *)sender
{
    if (sender.tag == 100) {
        AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        UIWindow *window = app.window;
        
        [UIView animateWithDuration:.3 animations:^{
            window.alpha = 0;
            window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
        } completion:^(BOOL finished) {
            exit(0);
        }];
    }else{
//        if (((AppDelegate *)[UIApplication sharedApplication].delegate).isStartLoad) {
//            [[NSNotificationCenter defaultCenter] postNotificationName:@"agreeNotification" object:nil];
//        }
        NSUserDefaults *userDf = [NSUserDefaults standardUserDefaults];
        [userDf setValue:@"yes" forKey:@"agreeNotification"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        if (_agBlock) {
            _agBlock();
        }
    }
}

- (CGSize)sizeWithString:(NSString *)string font:(UIFont *)font CGsize:(CGSize)CGsize
{
    CGRect rect = [string boundingRectWithSize:CGsize//限制最大的宽度和高度
                                       options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading  |NSStringDrawingUsesLineFragmentOrigin//采用换行模式
                                    attributes:@{NSFontAttributeName: font}//传入的字体字典
                                       context:nil];
    
    return rect.size;
}

@end

@implementation JSTNoContentView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kWhiteColor;
        [self createContent];
    }
    return self;
}

- (void) createContent
{
    UILabel *label = [[UILabel alloc] initWithFrame:self.bounds];
    label.text =  @"暂无记录";
    label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:label];
}

@end
